package com.asia.algorithmcode.binaryTree;

import java.util.ArrayList;
import java.util.List;

/**
 * @DESCRIPTION: 113、路径总和
 * @USER: wanfu
 * @DATE: 2025/5/13 星期二 16:44
 */
public class PathSum {

    public static void main(String[] args) {
        TreeNode t3 = new TreeNode(3);
        TreeNode t9 = new TreeNode(9);
        TreeNode t20 = new TreeNode(20);
        TreeNode t15 = new TreeNode(15);
        TreeNode t7 = new TreeNode(7);

        t3.left = t9;
        t3.right = t20;
        t20.left = t15;
        t20.right = t7;

        System.out.println(new PathSum().pathSum(t3, 30));
    }

    List<List<Integer>> list = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<Integer> inner = new ArrayList<>();
        dfs(root, targetSum, inner);
        return list;
    }

    private void dfs(TreeNode root, int targetSum, List<Integer> inner) {
        if (root == null) {
            return;
        }
        inner.add(root.val);
        if (root.left == null && root.right == null && targetSum == root.val) {
            list.add(new ArrayList<>(inner));
        }
        dfs(root.left, targetSum - root.val, inner);
        dfs(root.right, targetSum - root.val, inner);
        inner.remove(inner.size() - 1);
    }


}
